import { reactive } from "vue";
import { delayTask } from "@/utils/timer";

export const useSwipe = (callback?: (index: number) => void) => {
  let timer = 0;

  const swipeData = reactive({
    index: 0, // 只初始化时可设置，其他时候设置会出现预期之外的情况
    current: 0, // 操作这个值
  });

  const swipeChange = (e: MiniApp.Event<{ current: number }>) => {
    const current = e.detail.current;
    console.log("swipeChange", current);
    swipeData.index = current;
    callback?.(current);

    clearTimeout(timer);
    timer = delayTask(500, () => {
      console.log("swipeChange delayTask", current);
      swipeData.current = current;
    });
  };

  return { swipeData, swipeChange };
};
